﻿using LinqToAmazonSL.AWS;
using System.Globalization;

namespace LinqToAmazonSL
{
    public class AmazonBook : AmazonItem
    {
        #region IAmazonItem Members

        public string Author { get; set; }
        public string Creator { get; set; }
        public string Edition { get; set; }
        public string ISBN { get; set; }
        public string ASIN { get; set; }
        public string Label { get; set; }
        public string NumberOfPages { get; set; }
        public string ProductGroup { get; set; }
        public string PublicationDate { get; set; }
        public string ReadingLevel { get; set; }

        #endregion

        //Function to save the results in the "AmazonBook"
        public override void LoadFromAmazonResultItem(Item resultItem)
        {
            Author = string.Empty;
            Creator = string.Empty;
            PackageDimensions = string.Empty;
            Price = string.Empty;
            LowestNewPrice = string.Empty;
            LowestUsedPrice = string.Empty;
            Image = null;

            if (resultItem.ItemAttributes.Author != null && resultItem.ItemAttributes.Author.Length > 0)
            {
                int index = 0;
                while (index < resultItem.ItemAttributes.Author.Length - 1)
                {
                    Author += resultItem.ItemAttributes.Author[index] + ", ";
                    index++;
                }
                Author += resultItem.ItemAttributes.Author[index];
            }
            if (resultItem.ItemAttributes.Creator != null)
            {
                int index = 0;
                while (index < resultItem.ItemAttributes.Creator.Length - 1)
                {
                    Creator += "Role: " + resultItem.ItemAttributes.Creator[index].Role + ", Value: " + resultItem.ItemAttributes.Creator[index].Value + "; ";
                    index++;
                }
                Creator += "Role: " + resultItem.ItemAttributes.Creator[index].Role + ", Value: " + resultItem.ItemAttributes.Creator[index].Value;
            }
            ASIN = resultItem.ASIN ?? string.Empty;
            DetailPage = resultItem.DetailPageURL ?? string.Empty;
            Binding = resultItem.ItemAttributes.Binding ?? string.Empty;
            EAN = resultItem.ItemAttributes.EAN ?? string.Empty;
            Edition = resultItem.ItemAttributes.Edition ?? string.Empty;
            ISBN = resultItem.ItemAttributes.ISBN ?? string.Empty;
            Label = resultItem.ItemAttributes.Label ?? string.Empty;
            Manufacturer = resultItem.ItemAttributes.Manufacturer ?? string.Empty;
            NumberOfItems = resultItem.ItemAttributes.NumberOfItems ?? string.Empty;
            NumberOfPages = resultItem.ItemAttributes.NumberOfPages ?? string.Empty;
            if (resultItem.ItemAttributes.PackageDimensions != null)
            {
                if (resultItem.ItemAttributes.PackageDimensions.Height!=null)
                    PackageDimensions += "Height: " + resultItem.ItemAttributes.PackageDimensions.Height.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.PackageDimensions.Width!=null)
                    PackageDimensions += "Width: " + resultItem.ItemAttributes.PackageDimensions.Width.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.PackageDimensions.Length!=null)
                    PackageDimensions += "Length: " + resultItem.ItemAttributes.PackageDimensions.Length.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.PackageDimensions.Weight!=null)
                    PackageDimensions += "Weight: " + resultItem.ItemAttributes.PackageDimensions.Weight.Value.ToString(CultureInfo.CurrentCulture);
            }
            if (resultItem.OfferSummary != null && resultItem.OfferSummary.LowestUsedPrice != null)
            {
                TotalUsed = resultItem.OfferSummary.TotalUsed;
                LowestUsedPrice = resultItem.OfferSummary.LowestUsedPrice.FormattedPrice;
            }
            if (resultItem.OfferSummary != null && resultItem.OfferSummary.LowestNewPrice != null)
            {
                TotalNew = resultItem.OfferSummary.TotalNew;
                LowestNewPrice = resultItem.OfferSummary.LowestNewPrice.FormattedPrice;
            }
            if (resultItem.OfferSummary != null && resultItem.OfferSummary.LowestCollectiblePrice != null)
            {
                TotalCollectible = resultItem.OfferSummary.TotalCollectible;
                LowestCollectiblePrice = resultItem.OfferSummary.LowestCollectiblePrice.FormattedPrice;
            }
            if (resultItem.ItemAttributes != null && resultItem.ItemAttributes.ListPrice != null)
                Price = resultItem.ItemAttributes.ListPrice.FormattedPrice;
            
            ProductGroup = resultItem.ItemAttributes.ProductGroup ?? string.Empty;
            ProductTypeName = resultItem.ItemAttributes.ProductTypeName ?? string.Empty;
            PublicationDate = resultItem.ItemAttributes.PublicationDate ?? string.Empty;
            Publisher = resultItem.ItemAttributes.Publisher ?? string.Empty;
            ReadingLevel = resultItem.ItemAttributes.ReadingLevel ?? string.Empty;
            ReleaseDate = resultItem.ItemAttributes.ReleaseDate ?? string.Empty;
            Studio = resultItem.ItemAttributes.Studio ?? string.Empty;
            Title = resultItem.ItemAttributes.Title ?? string.Empty;
            if (resultItem.LargeImage != null)
            {
                Image = new Image();
                Image.Height = resultItem.LargeImage.Height;
                Image.Width = resultItem.LargeImage.Width;
                Image.URL = resultItem.LargeImage.URL;
            }
            else if (resultItem.MediumImage != null)
            {
                Image = new Image();
                Image.Height = resultItem.MediumImage.Height;
                Image.Width = resultItem.MediumImage.Width;
                Image.URL = resultItem.MediumImage.URL;
            }
            else if (resultItem.SmallImage != null)
            {
                Image = new Image();
                Image.Height = resultItem.SmallImage.Height;
                Image.Width = resultItem.SmallImage.Width;
                Image.URL = resultItem.SmallImage.URL;
            }
        }

        //Comparing two objects, the implicit and the past, returning true if they are equal
        public bool EqualsBook(object value)
        {
            if (value == null) return false;
            
            if (this.GetType() != value.GetType()) return false;

            AmazonBook book = (AmazonBook)value;

            if (!Author.Equals(book.Author)) return false;
            if (!Binding.Equals(book.Binding)) return false;
            if (!Creator.Equals(book.Creator)) return false;
            if (!EAN.Equals(book.EAN)) return false;
            if (!Edition.Equals(book.Edition)) return false;
            if (!ISBN.Equals(book.ISBN)) return false;
            if (!Label.Equals(book.Label)) return false;
            if (!Manufacturer.Equals(book.Manufacturer)) return false;
            if (!NumberOfItems.Equals(book.NumberOfItems)) return false;
            if (!NumberOfPages.Equals(book.NumberOfPages)) return false;
            if (!PackageDimensions.Equals(book.PackageDimensions)) return false;
            if (!Price.Equals(book.Price)) return false;
            if (!ProductGroup.Equals(book.ProductGroup)) return false;
            if (!ProductTypeName.Equals(book.ProductTypeName)) return false;
            if (!PublicationDate.Equals(book.PublicationDate)) return false;
            if (!Publisher.Equals(book.Publisher)) return false;
            if (!ReadingLevel.Equals(book.ReadingLevel)) return false;
            if (!ReleaseDate.Equals(book.ReleaseDate)) return false;
            if (!Studio.Equals(book.Studio)) return false;
            if (!Title.Equals(book.Title)) return false;
           
            return true;
        } 
    }
}
